﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
    CodeBehind="ReceivedProducts.aspx.cs" Inherits="MonkeyWarranty.Web.WarrantyTickets.ReceivedProducts" %>

<%@ Import Namespace="Resources" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <title>
        <%=GlobalStrings.ProcessReceivedProducts %></title>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <div class="row">
        <div class="col-lg-12">
            <h3><%=GlobalStrings.ProcessReceivedProducts %></h3>
        </div>
    </div>
    
    <div class="row">
        <div class="col-lg-12">
            <div class="well">
                <div class="form-inline">
                    <input class="form-control" type="text" id="ticketcode" placeholder="<%=GlobalStrings.TicketCode %>" />
                    <input class="form-control" type="text" id="code" placeholder="<%=GlobalStrings.ProductCode %>" />
                    <input class="form-control" type="text" id="name" placeholder="<%=GlobalStrings.ProductName %>" />
                    <button class="btn btn-default" type="button" id="LoadRecordsButton"><i class="fa fa-search"></i></button>
                    <button class="btn btn-default" type="button" id="ResetButton"><i class="fa fa-refresh"></i></button>
                </div>
            </div>

            <div id="ProductsTableContainer"></div>
        </div>
    </div>
    
    <div class="row">
        <div class="col-lg-12">
            <p></p>
            <input id="btnSendToTechnical" type="button" value="<%= GlobalStrings.SendBackCustomer %>"
                class="btn btn-success" onclick=" javascript:SendBackCustomer(); " />
        </div>
    </div>

    <script type="text/javascript">
        $(function () {
            $("#menu-warranty").addClass("active");
            $("#menu1-warranty").addClass("collapse in");
            $("#menu1-received").addClass("active");
            LoadProducts();
        });
    </script>
    <script type="text/javascript">
        function LoadProducts() {
            $('#ProductsTableContainer').jtable({
                paging: true,
                pageSize: 15,
                sorting: true,
                defaultSorting: 'Name ASC',
                actions: {
                    listAction: 'ReceivedProducts.aspx/ProductList'
                },
                fields: {
                    Id: {
                        key: true,
                        create: false,
                        edit: false,
                        list: false
                    },
                    Select: {
                        title: '<input type="checkbox" id="chkAllProducts" onclick="javascript:ToggleAll();" />',
                        width: '1%',
                        sorting: false,
                        display: function (data) {
                            return '<input name="products" value="' + data.record.Id + '" type="checkbox" onclick="javascript:SelectProduct(' + data.record.Id + ');" />';
                        }
                    },
                    //WarrantyProductState: { title: '<%= GlobalStrings.Status %>', sorting: false },
                    TicketCode: { title: '<%= GlobalStrings.TicketCode %>', sorting: false },
                    <%--ReceivedFromName: { title: '<%= GlobalStrings.ReceivedFrom %>', sorting: false },
                    DealerEmployeeName: { title: '<%= GlobalStrings.ReceivedFromName %>', width: '20%', sorting: false },--%>
                    CustomerName: { title: '<%= GlobalStrings.Customers %>', sorting: false },
                    Phone: { title: '<%= GlobalStrings.Phone %>', sorting: false },
                    Code: {
                        title: '<%= GlobalStrings.ProductCode %>'
                    },
                    Name: {
                        title: '<%= GlobalStrings.ProductName %>'
                    },
                    Model: {
                        title: '<%= GlobalStrings.Model %>',
                        sorting: false
                    },
                    <%--ManufacturerName: {
                        title: '<%= GlobalStrings.Manufacturers %>',
                        sorting: false
                    },--%>
                    Serial: {
                        title: '<%= GlobalStrings.Serial %>',
                        sorting: false
                    },
                    Quantity: {
                        width: '5%',
                        title: '<%= GlobalStrings.Quantity %>',
                        sorting: false
                    }
                    <%--WarrantyTypeName: {
                        title: '<%= GlobalStrings.WarrantyType %>',
                        sorting: false
                    }--%>
                }
            });

            //Re-load records when user click 'load records' button.
            $('#LoadRecordsButton').click(function (e) {
                e.preventDefault();
                $('#ProductsTableContainer').jtable('load', {
                    name: $('#name').val(),
                    code: $('#code').val(),
                    ticketCode: $('#ticketcode').val()
                });
            });

            // Reset button
            $('#ResetButton').click(function (e) {
                e.preventDefault();
                $('#name').val('');
                $('#code').val('');
                $('#ticketcode').val('');
                $('#LoadRecordsButton').click();
            });

            //Load all records when page is first shown
            $('#LoadRecordsButton').click();
        }

        function SelectProduct(id) {
            var products = $('input[name=products]');
            for (var i = 0; i < products.length; i++) {
                if (products[i].checked == false) {
                    $('#chkAllProducts')[0].checked = false;
                    return;
                }
            }
            $('#chkAllProducts')[0].checked = true;
        }
    </script>
    <script type="text/javascript">
        function SendBackCustomer() {
            var products = $('input[name=products]');
            var productIds = new Array();
            for (var i = 0; i < products.length; i++) {
                if (products[i].checked)
                    productIds.push(products[i].value);
            }

            if (productIds.length == 0) {
                alert("<%=GlobalStrings.SelectProduct %>");
                return;
            }

            $.ajax({
                type: "POST",
                url: "ReceivedProducts.aspx/SendBackCustomer",
                data: JSON.stringify({ productIds: productIds }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(data) {
                    LoadProducts();
                },
                failure: function(errMsg) {
                    alert(errMsg);
                }
            });
        }

        function ToggleAll() {
            var chkAll = $('#chkAllProducts')[0];
            var products = $('input[name=products]');
            for (var i = 0; i < products.length; i++) {
                products[i].checked = chkAll.checked;
            }
        }
    </script>
</asp:Content>
